#include <signal.h>
#include "front_thread.h"
#include "front_server.h"
#include "gf_thread.h"
#include "gf_server.h"
#include "Log.h"

using namespace std;

int main(int argc, char **argv)
{
	string path = "./logs";
	string log_level = "DEBUG";
	if (!init_logger(path, log_level)) {
		cout << "logger init failed log[@path | @level]" << endl;
		return -1;
	}

	vector<server *> servers;

	string ip = "199.155.122.11";
	string port = "45500";
	address addr;
	//front 
	front_server *front = new front_server;
	port = "45500";
	addr.set_address(ip,port);
	front->set_front_address(addr);
	port = "45510";
	addr.set_port(port);
	front->set_back_address(addr);
	//add gf_extract
	port = "45501";
	addr.set_port(port);
	front->set_gf_extract(addr);
	//add gf_merge
	port = "45502";
	addr.set_port(port);
	front->set_gf_merge(addr);
	
	front->set_thread_num(1);
	front_thread *ft = NULL;
	for(int i = 0; i < front->get_thread_pool()->get_thread_num(); i++){
		ft = new front_thread(front->get_thread_pool());
		front->get_thread_pool()->add_thread(ft);
	}
	servers.push_back(front);

	//gf_extract
	gf_extract_server *gf_extract = new gf_extract_server;
	port = "45501";
	addr.set_port(port);
	gf_extract->set_front_address(addr);
	port = "45511";
	addr.set_port(port);
	gf_extract->set_back_address(addr);

	gf_extract->set_thread_num(1);
	gf_extract_thread *gft = NULL;
	for(int i = 0; i < gf_extract->get_thread_pool()->get_thread_num(); i++){
		gft = new gf_extract_thread(gf_extract->get_thread_pool());
		gf_extract->get_thread_pool()->add_thread(gft);
	}
	servers.push_back(gf_extract);

	//gf_merge
	gf_merge_server *gf_merge = new gf_merge_server;
	port = "45502";
	addr.set_port(port);
	gf_merge->set_front_address(addr);
	port = "45512";
	addr.set_port(port);
	gf_merge->set_back_address(addr);

	//add gf_match
	port = "45503";
	addr.set_port(port);
	gf_merge->add_gf_match(addr);

	gf_merge->set_thread_num(1);
	gf_merge_thread *gf_merge_t = NULL;
	for(int i = 0; i < gf_merge->get_thread_pool()->get_thread_num(); i++){
		gf_merge_t = new gf_merge_thread(gf_merge->get_thread_pool());
		gf_merge->get_thread_pool()->add_thread(gf_merge_t);
	}
	servers.push_back(gf_merge);

	//gf_match
	gf_match_server *gf_match = new gf_match_server;
	port = "45503";
	addr.set_port(port);
	gf_match->set_front_address(addr);
	port = "45513";
	addr.set_port(port);
	gf_match->set_back_address(addr);

	gf_match->set_thread_num(1);
	gf_match_thread *gmt = NULL;
	for(int i = 0; i < gf_match->get_thread_pool()->get_thread_num(); i++){
		gmt = new gf_match_thread(gf_match->get_thread_pool());
		gf_match->get_thread_pool()->add_thread(gmt);
	}
	servers.push_back(gf_match);

	//start all server
	for (vector<server *>::iterator it = servers.begin(); it != servers.end(); it++)
		(*it)->start();


	while(1) sleep(1);
	return 0;
}

